#include <assert.h>
#include "scene.h"
#include "ctrlboard.h"
#include "mg_Lcd_Driver.h"
#include "mg_Device.h"
#include "mg_Rs485Protocol.h"
#include "mg_Rotate.h"
/* widgets:
Layer_MainMenu5
Background_Mainmenu5
Container_Temp
Container_MainMenu5_4
Text_MM5_Page4
Text_MM5_Name2_4
Text_MM5_Name1_4
PopupButton2_MainMenu5_4
PopupButton1_MainMenu5_4
Container_MainMenu5_3
Text_MM5_Page3
Text_MM5_Name2_3
Text_MM5_Name1_3
PopupButton2_MainMenu5_3
PopupButton1_MainMenu5_3
Container_MainMenu5_2
Text_MM5_Page2
Text_MM5_Name2_2
Text_MM5_Name1_2
PopupButton2_MainMenu5_2
PopupButton1_MainMenu5_2
Container_MainMenu5_1
Text_MM5_Page1
Text_MM5_Name2_1
Text_MM5_Name1_1
PopupButton2_MainMenu5_1
PopupButton1_MainMenu5_1
Container_MainMenu5_0
Text_MM5_Page0
Text_MM5_Name2_0
Text_MM5_Name1_0
PopupButton2_MainMenu5_0
PopupButton1_MainMenu5_0
CoverFlow_Mainmenu5
Container_Config
Text17
PopupButton_Config
Container_MainMenu5_12
Icon_Cross_12
Text_MM5_Page12
Text_MM5_Name4_12
Text_MM5_Name3_12
Text_MM5_Name2_12
Text_MM5_Name1_12
PopupButton4_MainMenu5_12
PopupButton3_MainMenu5_12
PopupButton2_MainMenu5_12
PopupButton1_MainMenu5_12
Container_MainMenu5_11
Icon_Cross_11
Text_MM5_Page11
Text_MM5_Name4_11
Text_MM5_Name3_11
Text_MM5_Name2_11
Text_MM5_Name1_11
PopupButton4_MainMenu5_11
PopupButton3_MainMenu5_11
PopupButton2_MainMenu5_11
PopupButton1_MainMenu5_11
Container_MainMenu5_10
Icon_Cross_10
Text_MM5_Page10
Text_MM5_Name4_10
Text_MM5_Name3_10
Text_MM5_Name2_10
Text_MM5_Name1_10
PopupButton4_MainMenu5_10
PopupButton3_MainMenu5_10
PopupButton2_MainMenu5_10
PopupButton1_MainMenu5_10
Container_MainMenu5_9
Icon_Cross_9
Text_MM5_Page9
Text_MM5_Name4_9
Text_MM5_Name3_9
Text_MM5_Name2_9
Text_MM5_Name1_9
PopupButton4_MainMenu5_9
PopupButton3_MainMenu5_9
PopupButton2_MainMenu5_9
PopupButton1_MainMenu5_9
Container_MainMenu5_8
Icon_Cross_8
Text_MM5_Page8
Text_MM5_Name4_8
Text_MM5_Name3_8
Text_MM5_Name2_8
Text_MM5_Name1_8
PopupButton4_MainMenu5_8
PopupButton3_MainMenu5_8
PopupButton2_MainMenu5_8
PopupButton1_MainMenu5_8
Container_MainMenu5_7
Icon_Cross_7
Text_MM5_Page7
Text_MM5_Name4_7
Text_MM5_Name3_7
Text_MM5_Name2_7
Text_MM5_Name1_7
PopupButton4_MainMenu5_7
PopupButton3_MainMenu5_7
PopupButton2_MainMenu5_7
PopupButton1_MainMenu5_7
Container_MainMenu5_6
Icon_Cross_6
Text_MM5_Page6
Text_MM5_Name4_6
Text_MM5_Name3_6
Text_MM5_Name2_6
Text_MM5_Name1_6
PopupButton4_MainMenu5_6
PopupButton3_MainMenu5_6
PopupButton2_MainMenu5_6
PopupButton1_MainMenu5_6
Container_MainMenu5_5
Icon_Cross_5
Text_MM5_Page5
Text_MM5_Name4_5
Text_MM5_Name3_5
Text_MM5_Name2_5
Text_MM5_Name1_5
PopupButton4_MainMenu5_5
PopupButton3_MainMenu5_5
PopupButton2_MainMenu5_5
PopupButton1_MainMenu5_5
*/
#define PAGE_NUM 8
static ITUBackground* Background_Mainmenu5;
static ITUCoverFlow* CoverFlow_Mainmenu5;
static ITUContainer* Container_MainMenu5[PAGE_NUM];
static ITUPopupButton* PopupButton1_MainMenu5[PAGE_NUM];
static ITUPopupButton* PopupButton2_MainMenu5[PAGE_NUM];
static ITUPopupButton* PopupButton3_MainMenu5[PAGE_NUM];
static ITUPopupButton* PopupButton4_MainMenu5[PAGE_NUM];
static ITUText* Text_MM5_Name1[PAGE_NUM];
static ITUText* Text_MM5_Name2[PAGE_NUM];
static ITUText* Text_MM5_Name3[PAGE_NUM];
static ITUText* Text_MM5_Name4[PAGE_NUM];
static ITUText* Text_MM5_Page[PAGE_NUM];
static bool Is_HaveDevice = false;
bool MainMenu5OnEnter(ITUWidget* widget, char* param)
{
	uint8_t i, PageNum_D = 0;
	char PageNum[3], Page[10];
	char DeviceState_Text[20];
	char TempName[64], TempNum[64];
	UTF8_TWO_LINE DeviceTempName;
	if (!Background_Mainmenu5)
	{
		Background_Mainmenu5 = ituSceneFindWidget(&theScene, "Background_Mainmenu5");
		assert(Background_Mainmenu5);
		CoverFlow_Mainmenu5 = ituSceneFindWidget(&theScene, "CoverFlow_Mainmenu5");
		assert(CoverFlow_Mainmenu5);
		for (i = 0; i < PAGE_NUM; i++)//加载小部件
		{
			//Container
			strcpy(TempName, "Container_MainMenu5_");
			strcat(TempName, itoa(i + 5, TempNum, 10));
			Container_MainMenu5[i] = ituSceneFindWidget(&theScene, TempName);
			assert(Container_MainMenu5[i]);
			//PopupButton
			strcpy(TempName, "PopupButton1_MainMenu5_");
			strcat(TempName, itoa(i + 5, TempNum, 10));
			PopupButton1_MainMenu5[i] = ituSceneFindWidget(&theScene, TempName);
			assert(PopupButton1_MainMenu5[i]);
			strcpy(TempName, "PopupButton2_MainMenu5_");
			strcat(TempName, itoa(i + 5, TempNum, 10));
			PopupButton2_MainMenu5[i] = ituSceneFindWidget(&theScene, TempName);
			assert(PopupButton2_MainMenu5[i]);
			strcpy(TempName, "PopupButton3_MainMenu5_");
			strcat(TempName, itoa(i + 5, TempNum, 10));
			PopupButton3_MainMenu5[i] = ituSceneFindWidget(&theScene, TempName);
			assert(PopupButton3_MainMenu5[i]);
			strcpy(TempName, "PopupButton4_MainMenu5_");
			strcat(TempName, itoa(i + 5, TempNum, 10));
			PopupButton4_MainMenu5[i] = ituSceneFindWidget(&theScene, TempName);
			assert(PopupButton4_MainMenu5[i]);
			//Text_MM_Name1
			strcpy(TempName, "Text_MM5_Name1_");
			strcat(TempName, itoa(i + 5, TempNum, 10));
			Text_MM5_Name1[i] = ituSceneFindWidget(&theScene, TempName);
			assert(Text_MM5_Name1[i]);
			strcpy(TempName, "Text_MM5_Name2_");
			strcat(TempName, itoa(i + 5, TempNum, 10));
			Text_MM5_Name2[i] = ituSceneFindWidget(&theScene, TempName);
			assert(Text_MM5_Name2[i]);
			strcpy(TempName, "Text_MM5_Name3_");
			strcat(TempName, itoa(i + 5, TempNum, 10));
			Text_MM5_Name3[i] = ituSceneFindWidget(&theScene, TempName);
			assert(Text_MM5_Name3[i]);
			strcpy(TempName, "Text_MM5_Name4_");
			strcat(TempName, itoa(i + 5, TempNum, 10));
			Text_MM5_Name4[i] = ituSceneFindWidget(&theScene, TempName);
			assert(Text_MM5_Name4[i]);
			//Text_MM_Page
			strcpy(TempName, "Text_MM5_Page");
			strcat(TempName, itoa(i + 5, TempNum, 10));
			Text_MM5_Page[i] = ituSceneFindWidget(&theScene, TempName);
			assert(Text_MM5_Page[i]);
		}
	}
	ituWidgetSetVisible(CoverFlow_Mainmenu5, false);//设置为显示
	for (i = 1; i <= DeviceNumMax; i++)//获取总页数
	{
		if (TFT_Device[i].DeviceIsAllocation == true)
			PageNum_D++;
	}
	//将总页数转成字符串
	if (PageNum_D % 4 == 0)
		PageNum_D /= 4;
	else
		PageNum_D = PageNum_D / 4 + 1;
	itoa(PageNum_D, PageNum, 10);
	//对每个按键进行编辑
	Is_HaveDevice = false;
	PageNum_D = 0;//页码归零
	for (i = 0; i < PAGE_NUM; i++)
	{
		if (i == 7)
		{
			if (TFT_Device[i * 4 + 1].DeviceIsAllocation == true ||
				TFT_Device[i * 4 + 2].DeviceIsAllocation == true)
			{
				if (TFT_Device[i * 4 + 1].DeviceEventType == 1)//设备名称
					ituTextSetString(Text_MM5_Name1[i], TFT_Device[i * 4 + 1].TFTSCEQ.TFT_Device.DeviceName);
				else
					ituTextSetString(Text_MM5_Name1[i], TFT_Device[i * 4 + 1].TFTSCEQ.TFT_Scene.DeviceName);

				if (TFT_Device[i * 4 + 2].DeviceEventType == 1)//设备名称
					ituTextSetString(Text_MM5_Name2[i], TFT_Device[i * 4 + 2].TFTSCEQ.TFT_Device.DeviceName);
				else
					ituTextSetString(Text_MM5_Name2[i], TFT_Device[i * 4 + 2].TFTSCEQ.TFT_Scene.DeviceName);

				Is_HaveDevice = true;
				PageNum_D++;
				//页码显示
				itoa(PageNum_D, Page, 10);
				strcat(Page, "/");
				strcat(Page, PageNum);
				ituTextSetString(Text_MM5_Page[i], Page);
				printf("Page : %s\n", Page);
				ituWidgetSetVisible(Container_MainMenu5[i], true);//设置为隐藏
			}
			else
			{
				ituWidgetSetVisible(Container_MainMenu5[i], false);//设置为隐藏
			}
		}
		else
		{
			if (TFT_Device[i * 4 + 1].DeviceIsAllocation == true ||
				TFT_Device[i * 4 + 2].DeviceIsAllocation == true ||
				TFT_Device[i * 4 + 3].DeviceIsAllocation == true ||
				TFT_Device[i * 4 + 4].DeviceIsAllocation == true)
			{
				if (TFT_Device[i * 4 + 1].DeviceEventType == 1)//设备名称
					ituTextSetString(Text_MM5_Name1[i], TFT_Device[i * 4 + 1].TFTSCEQ.TFT_Device.DeviceName);
				else
					ituTextSetString(Text_MM5_Name1[i], TFT_Device[i * 4 + 1].TFTSCEQ.TFT_Scene.DeviceName);

				if (TFT_Device[i * 4 + 2].DeviceEventType == 1)//设备名称
					ituTextSetString(Text_MM5_Name2[i], TFT_Device[i * 4 + 2].TFTSCEQ.TFT_Device.DeviceName);
				else
					ituTextSetString(Text_MM5_Name2[i], TFT_Device[i * 4 + 2].TFTSCEQ.TFT_Scene.DeviceName);

				if (TFT_Device[i * 4 + 3].DeviceEventType == 1)//设备名称
					ituTextSetString(Text_MM5_Name3[i], TFT_Device[i * 4 + 3].TFTSCEQ.TFT_Device.DeviceName);
				else
					ituTextSetString(Text_MM5_Name3[i], TFT_Device[i * 4 + 3].TFTSCEQ.TFT_Scene.DeviceName);

				if (TFT_Device[i * 4 + 4].DeviceEventType == 1)//设备名称
					ituTextSetString(Text_MM5_Name4[i], TFT_Device[i * 4 + 4].TFTSCEQ.TFT_Device.DeviceName);
				else
					ituTextSetString(Text_MM5_Name4[i], TFT_Device[i * 4 + 4].TFTSCEQ.TFT_Scene.DeviceName);

				Is_HaveDevice = true;
				PageNum_D++;
				//页码显示
				itoa(PageNum_D, Page, 10);
				strcat(Page, "/");
				strcat(Page, PageNum);
				ituTextSetString(Text_MM5_Page[i], Page);
				printf("Page : %s\n", Page);
				ituWidgetSetVisible(Container_MainMenu5[i], true);//设置为隐藏
			}
			else
			{
				ituWidgetSetVisible(Container_MainMenu5[i], false);//设置为隐藏
			}
		}
	}
	if (Is_HaveDevice == false)//没有设备
	{
		ituWidgetSetVisible(Container_MainMenu5[0], true);//设置为显示
#ifndef WIN32
		ituTextSetString(Text_MM5_Name1[0], "未配置");
		ituTextSetString(Text_MM5_Name2[0], "未配置");
		ituTextSetString(Text_MM5_Name3[0], "未配置");
		ituTextSetString(Text_MM5_Name4[0], "未配置");
		ituTextSetString(Text_MM5_Page[0], "1/1");
#else
		ituTextSetString(Text_MM5_Name1[0], "暂未配置");
		ituTextSetString(Text_MM5_Name2[0], "暂未配置");
		ituTextSetString(Text_MM5_Name3[0], "暂未配置");
		ituTextSetString(Text_MM5_Name4[0], "暂未配置");
		ituTextSetString(Text_MM5_Page[0], "1/1");
#endif
		ituCoverFlowGoto(CoverFlow_Mainmenu5, 0);
	}
	ituWidgetSetVisible(CoverFlow_Mainmenu5, true);//设置为显示
	ituWidgetSetVisible(Background_Mainmenu5, true);//设置为显示
	Set_Rotate_Block(120);//设置间隔刻度
	return true;
}

bool MainMenu5OnLeave(ITUWidget* widget, char* param)
{
	ituWidgetSetVisible(Background_Mainmenu5, false);//设置为隐藏
    return true;
}

bool MainMenu5OnTimer(ITUWidget* widget, char* param)
{
	static uint8_t TempIndex = 1;
	uint8_t i, PageNum_D = 0;
	char PageNum[3], Page[10];
	char DeviceState_Text[20];
	UTF8_TWO_LINE DeviceTempName;

	//用户操作
	if (Action_Cmd.User_Action == Left_Slide || Action_Cmd.User_Action == Anticlockwise_Rotation)
	{
		if (CoverFlowCheckIdle(CoverFlow_Mainmenu5))
		{
			ituCoverFlowPrev(CoverFlow_Mainmenu5);
		}
		Clear_User_Action();
	}
	else if (Action_Cmd.User_Action == Right_Slide || Action_Cmd.User_Action == Clockwise_Rotation)
	{
		if (CoverFlowCheckIdle(CoverFlow_Mainmenu5))
		{
			ituCoverFlowNext(CoverFlow_Mainmenu5);
		}
		Clear_User_Action();
	}

	//设备配置更新
	if (MessageFlag[MSG_GUI_CONFIG_UPDATE] == ON)
	{
		ituWidgetSetVisible(CoverFlow_Mainmenu5, false);//设置为隐藏
		for (i = 1; i <= DeviceNumMax; i++)//获取总页数
		{
			if (TFT_Device[i].DeviceIsAllocation == true)
				PageNum_D++;
		}
		//将总页数转成字符串
		if (PageNum_D % 4 == 0)
			PageNum_D /= 4;
		else
			PageNum_D = PageNum_D / 4 + 1;
		itoa(PageNum_D, PageNum, 10);
		//对每个按键进行编辑
		Is_HaveDevice = false;
		PageNum_D = 0;//页码归零
		for (i = 0; i < PAGE_NUM; i++)
		{
			if (i == 7)
			{
				if (TFT_Device[i * 4 + 1].DeviceIsAllocation == true ||
					TFT_Device[i * 4 + 2].DeviceIsAllocation == true)
				{
					if (TFT_Device[i * 4 + 1].DeviceEventType == 1)//设备名称
						ituTextSetString(Text_MM5_Name1[i], TFT_Device[i * 4 + 1].TFTSCEQ.TFT_Device.DeviceName);
					else
						ituTextSetString(Text_MM5_Name1[i], TFT_Device[i * 4 + 1].TFTSCEQ.TFT_Scene.DeviceName);

					if (TFT_Device[i * 4 + 2].DeviceEventType == 1)//设备名称
						ituTextSetString(Text_MM5_Name2[i], TFT_Device[i * 4 + 2].TFTSCEQ.TFT_Device.DeviceName);
					else
						ituTextSetString(Text_MM5_Name2[i], TFT_Device[i * 4 + 2].TFTSCEQ.TFT_Scene.DeviceName);

					Is_HaveDevice = true;
					PageNum_D++;
					//页码显示
					itoa(PageNum_D, Page, 10);
					strcat(Page, "/");
					strcat(Page, PageNum);
					ituTextSetString(Text_MM5_Page[i], Page);
					printf("Page : %s\n", Page);
					ituWidgetSetVisible(Container_MainMenu5[i], true);//设置为隐藏
				}
				else
				{
					ituWidgetSetVisible(Container_MainMenu5[i], false);//设置为隐藏
				}
			}
			else
			{
				if (TFT_Device[i * 4 + 1].DeviceIsAllocation == true ||
					TFT_Device[i * 4 + 2].DeviceIsAllocation == true ||
					TFT_Device[i * 4 + 3].DeviceIsAllocation == true ||
					TFT_Device[i * 4 + 4].DeviceIsAllocation == true)
				{
					if (TFT_Device[i * 4 + 1].DeviceEventType == 1)//设备名称
						ituTextSetString(Text_MM5_Name1[i], TFT_Device[i * 4 + 1].TFTSCEQ.TFT_Device.DeviceName);
					else
						ituTextSetString(Text_MM5_Name1[i], TFT_Device[i * 4 + 1].TFTSCEQ.TFT_Scene.DeviceName);

					if (TFT_Device[i * 4 + 2].DeviceEventType == 1)//设备名称
						ituTextSetString(Text_MM5_Name2[i], TFT_Device[i * 4 + 2].TFTSCEQ.TFT_Device.DeviceName);
					else
						ituTextSetString(Text_MM5_Name2[i], TFT_Device[i * 4 + 2].TFTSCEQ.TFT_Scene.DeviceName);

					if (TFT_Device[i * 4 + 3].DeviceEventType == 1)//设备名称
						ituTextSetString(Text_MM5_Name3[i], TFT_Device[i * 4 + 3].TFTSCEQ.TFT_Device.DeviceName);
					else
						ituTextSetString(Text_MM5_Name3[i], TFT_Device[i * 4 + 3].TFTSCEQ.TFT_Scene.DeviceName);

					if (TFT_Device[i * 4 + 4].DeviceEventType == 1)//设备名称
						ituTextSetString(Text_MM5_Name4[i], TFT_Device[i * 4 + 4].TFTSCEQ.TFT_Device.DeviceName);
					else
						ituTextSetString(Text_MM5_Name4[i], TFT_Device[i * 4 + 4].TFTSCEQ.TFT_Scene.DeviceName);

					Is_HaveDevice = true;
					PageNum_D++;
					//页码显示
					itoa(PageNum_D, Page, 10);
					strcat(Page, "/");
					strcat(Page, PageNum);
					ituTextSetString(Text_MM5_Page[i], Page);
					printf("Page : %s\n", Page);
					ituWidgetSetVisible(Container_MainMenu5[i], true);//设置为隐藏
				}
				else
				{
					ituWidgetSetVisible(Container_MainMenu5[i], false);//设置为隐藏
				}
			}
		}
		if (Is_HaveDevice == false)//没有设备
		{
			ituWidgetSetVisible(Container_MainMenu5[0], true);//设置为显示
#ifndef WIN32
			ituTextSetString(Text_MM5_Name1[0], "未配置");
			ituTextSetString(Text_MM5_Name2[0], "未配置");
			ituTextSetString(Text_MM5_Name3[0], "未配置");
			ituTextSetString(Text_MM5_Name4[0], "未配置");
			ituTextSetString(Text_MM5_Page[0], "1/1");
#else
			ituTextSetString(Text_MM5_Name1[0], "暂未配置");
			ituTextSetString(Text_MM5_Name2[0], "暂未配置");
			ituTextSetString(Text_MM5_Name3[0], "暂未配置");
			ituTextSetString(Text_MM5_Name4[0], "暂未配置");
			ituTextSetString(Text_MM5_Page[0], "1/1");
#endif
			ituCoverFlowGoto(CoverFlow_Mainmenu5, 0);
		}
		ituWidgetSetVisible(CoverFlow_Mainmenu5, true);//设置为显示
		MessageFlag[MSG_GUI_CONFIG_UPDATE] = OFF;
	}
	//进入测试模式
	if (MessageFlag[MSG_HARDWARE_TEST] == ON)
	{
		MessageFlag[MSG_HARDWARE_TEST] = OFF;
		ITULayer* Layer_TouchTest = ituSceneFindWidget(&theScene, "Layer_TouchTest");
		assert(Layer_TouchTest);
		ituLayerGoto(Layer_TouchTest);
	}
	//切换彩单类型
	if (MessageFlag[MSG_GUI_SWITCH_MAINMENU] == ON)
	{
		MessageFlag[MSG_GUI_SWITCH_MAINMENU] = OFF;
		GotoMainMenu();
	}
    return true;
}

